﻿using DalatCooking.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadParentDropDown(DropDownList1, 0);
        }
    }
    static int level = -1;
    public static void LoadParentDropDown(DropDownList ddl, int parentID)
    {
        level++;
        System.Text.StringBuilder appender = new System.Text.StringBuilder();
        for (int j = 0; j < level; j++)
        {
            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;");
        }
        if (level > 0)
        {
            appender.Append("|__");
        }
        List<Category> list = CategoryManager.FindAll().FindAll(ca => ca.ParentId == parentID && ca.IsActive == true);
        for (int i = 0; i < list.Count; i++)
        {
            ListItem l = new ListItem();
            l.Value = list[i].CategoryId.ToString();
            l.Text = HttpUtility.HtmlDecode(appender.ToString()) + list[i].Name;
            l.Attributes.Add("data-image", "ThumbnailHandler.axd?img=" + list[i].Picture + "");
            l.Attributes.Add("data-description", HttpUtility.HtmlDecode(appender.ToString()) + list[i].Name);

            // ddl.Items.Add(new ListItem(HttpUtility.HtmlDecode(appender.ToString()) + list[i].Name, list[i].CategoryId.ToString()));

            ddl.Items.Add(l);
            LoadParentDropDown(ddl, list[i].CategoryId); // Goi de quy ham              
        }
        level--;
    }


    public  string LoadParentDropDown_XXX(int parentID)
    {
        level++;
        string html = "";
        System.Text.StringBuilder appender = new System.Text.StringBuilder();
        for (int j = 0; j < level; j++)
        {
            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;");
        }
        if (level > 0)
        {
            appender.Append("|__");
        }
        List<Category> list = CategoryManager.FindAll().FindAll(ca => ca.ParentId == parentID && ca.IsActive == true);
        for (int i = 0; i < list.Count; i++)
        {
            //  ddl.Items.Add(new ListItem(HttpUtility.HtmlDecode(appender.ToString()) + list[i].Name, list[i].CategoryId.ToString()));
            html += "<option value=\"" + list[i].CategoryId + "\" data-image=\"/ThumbnailHandler.axd?img=" + list[i].Picture + "\" data-description=\"" + list[i].Name + "\">" + HttpUtility.HtmlDecode(appender.ToString()) + list[i].Name + "</option>\n";
          
            LoadParentDropDown_XXX(list[i].CategoryId); // Goi de quy ham              
        }
        level--;

        return html;
    }
}